#ifndef DXSDKMESHLOADER_H_
#define DXSDKMESHLOADER_H_

#include "MeshLoader.h"
#include "SDKmesh.h"

namespace Loader
{
    class DXSdkMeshLoader : public MeshLoader
    {
    private:
        CDXUTSDKMesh* g_mesh10;
        ID3D10EffectTechnique* tech;

    public:
        DXSdkMeshLoader()
        {
            g_mesh10 = new CDXUTSDKMesh();
        }
        ~DXSdkMeshLoader()
        {
            Destroy();
        };

        HRESULT Create( ID3D10Device* pd3dDevice, const WCHAR* strFilename )
        {
            return g_mesh10->Create(pd3dDevice, strFilename, true);
        }

        void    Destroy()
        {
            g_mesh10->Destroy();
        }

        UINT    GetNumMaterials() const
        {
            return g_mesh10->GetNumMaterials();
        }
        UINT GetNumSubsets() const
        {
            return g_mesh10->GetNumSubsets(0);
        }

        void DrawSubset(ID3D10Device* pd3dDevice, UINT iSubset)
        {
            // Get the subset
            SDKMESH_SUBSET* pSubset = g_mesh10->GetSubset( 0, iSubset );

            D3D10_PRIMITIVE_TOPOLOGY PrimType = CDXUTSDKMesh::GetPrimitiveType10( ( SDKMESH_PRIMITIVE_TYPE )pSubset->PrimitiveType );
            pd3dDevice->IASetPrimitiveTopology( PrimType );
            pd3dDevice->DrawIndexed( ( UINT )pSubset->IndexCount, 0, ( UINT )pSubset->VertexStart );
        }

        D3DXVECTOR4 GetSubsetMaterialAmbient(UINT iSubset)
        {
            return g_mesh10->GetMaterial(g_mesh10->GetSubset(0, iSubset)->MaterialID)->Ambient;
        }
        D3DXVECTOR4 GetSubsetMaterialDiffuse(UINT iSubset)
        {
            return g_mesh10->GetMaterial(g_mesh10->GetSubset(0, iSubset)->MaterialID)->Diffuse;
        }
        D3DXVECTOR4 GetSubsetMaterialSpecular(UINT iSubset)
        {
            return g_mesh10->GetMaterial(g_mesh10->GetSubset(0, iSubset)->MaterialID)->Specular;
        }
        float GetSubsetMaterialAlpha(UINT iSubset)
        {
            return g_mesh10->GetMaterial(g_mesh10->GetSubset(0, iSubset)->MaterialID)->Power;
        }
        int GetSubsetMaterialSpecularPower(UINT iSubset)
        {
            return 32;
        }
        ID3D10ShaderResourceView* GetSubsetMaterialDiffuseTxRV(UINT iSubset)
        {
            return g_mesh10->GetMaterial(g_mesh10->GetSubset(0, iSubset)->MaterialID)->pDiffuseRV10;
        }
        WCHAR* GetSubsetMaterialName(UINT iSubset)
        {
            WCHAR strName[MAX_PATH];
            mbstowcs(strName, g_mesh10->GetMaterial(g_mesh10->GetSubset(0, iSubset)->MaterialID)->Name, MAX_PATH);
            return strName;
        }
        ID3D10EffectTechnique* GetSubsetMaterialTech(UINT iSubset)
        {
            return tech;
        }
        void SetMaterialTech(ID3D10Effect* g_pEffect10)
        {
            tech = g_pEffect10->GetTechniqueByName( "TexturedSpecular" );
        }
        void SetIAVertexIndexBuffer(ID3D10Device* pd3dDevice)
        {
            UINT Strides[1];
            UINT Offsets[1];
            ID3D10Buffer* pVB[1];
            pVB[0] = g_mesh10->GetVB10( 0, 0 );
            Strides[0] = ( UINT )g_mesh10->GetVertexStride( 0, 0 );
            Offsets[0] = 0;
            pd3dDevice->IASetVertexBuffers( 0, 1, pVB, Strides, Offsets );
            pd3dDevice->IASetIndexBuffer( g_mesh10->GetIB10( 0 ), g_mesh10->GetIBFormat10( 0 ), 0 );
        }
        float GetAutoViewDistance()
        {
            //TODO:
            return 800;
        }

    };
}

#endif //DXSDKMESHLOADER_H_